<template>
  <div id="user-manage-page">
    <!--搜索表单-->
    <a-form layout="inline" :model="searchParams" @finish="handleSearch">
      <a-form-item label="用户名">
        <a-input v-model:value="searchParams.username" placeholder="请输入用户名" allow-clear />
      </a-form-item>
      <a-form-item label="昵称">
        <a-input v-model:value="searchParams.nickname" placeholder="请输入用户昵称" allow-clear />
      </a-form-item>
      <a-form-item>
        <a-button type="primary" html-type="submit">查询</a-button>
      </a-form-item>
    </a-form>
    <div style="margin-bottom: 16px"></div>
    <a-table
      :columns="columns"
      :data-source="dataList"
      :pagination="pagination"
      @change="handleChange"
    >
      <template #bodyCell="{ column, record }">
        <template v-if="column.dataIndex === 'avatar'">
          <a-image :src="record.avatar" :width="64" />
        </template>
        <template v-if="column.dataIndex === 'userRole'">
          <div v-if="record.userRole === 'user'">
            <a-tag color="green">普通用户</a-tag>
          </div>
          <div v-if="record.userRole === 'admin'">
            <a-tag color="blue">管理员</a-tag>
          </div>
        </template>
        <template v-if="column.dataIndex === 'createTime'">
          {{ dayjs(record.createTime).format('YYYY-MM-DD HH:mm:ss') }}
        </template>
        <template v-if="column.dataIndex === 'updateTime'">
          {{ dayjs(record.updateTime).format('YYYY-MM-DD HH:mm:ss') }}
        </template>
        <template v-if="column.key === 'action'">
          <a-button danger @click="handleDelete(record.id)">删除</a-button>
        </template>
      </template>
    </a-table>
  </div>
</template>
<script lang="ts" setup>
import { computed, onMounted, reactive, ref } from 'vue'
import { deleteUserApi, getUserListApi } from '@/api/UserApi.ts'
import { message } from 'ant-design-vue'
import dayjs from 'dayjs'

const columns = [
  {
    title: 'id',
    dataIndex: 'id',
  },
  {
    title: '用户名',
    dataIndex: 'username',
  },
  {
    title: '昵称',
    dataIndex: 'nickname',
  },
  {
    title: '角色',
    dataIndex: 'userRole',
  },
  {
    title: '用户头像',
    dataIndex: 'avatar',
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
  },
  {
    title: '更新时间',
    dataIndex: 'updateTime',
  },
  {
    title: '操作',
    key: 'action',
  },
]

const dataList = ref<Object[]>([])
const total = ref(0)

// 搜索条件
const searchParams = reactive<any>({
  current: 1,
  size: 10,
  username: '',
  nickname: '',
})

// 分页参数
const pagination = computed(() => {
  return {
    current: searchParams.current,
    pageSize: searchParams.size,
    total: total.value,
    showSizeChanger: true,
    showTotal: (total: number) => `共 ${total} 条数据`,
  }
})

// 获取数据
const fetchData = async () => {
  const res = await getUserListApi({
    ...searchParams,
  })
  if (res.data.code === 0) {
    dataList.value = res.data.data.records ?? []
    total.value = res.data.data.total ?? 0
  } else {
    message.error('获取用户列表失败')
  }
}

const handleChange = (page: any) => {
  searchParams.current = page.current
  searchParams.size = page.pageSize
  fetchData()
}

onMounted(() => {
  fetchData()
})

const handleSearch = () => {
  searchParams.current = 1
  fetchData()
}

const handleDelete = async (id: any) => {
  if (!id) {
    return
  }
  const res = await deleteUserApi({ id })
  if (res.data.code == 0) {
    message.success("删除用户成功")
    fetchData()
  } else {
    message.error("删除失败")
  }

}
</script>
<style scoped>
#user-manage-page {
}
</style>
